import java.util.HashSet;
import java.util.Set;

/**
 * @Description: 快乐数
 * https://leetcode.cn/problems/happy-number/
 * @Date:Create in 21:43 2022/7/2
 */
public class isHappy0202 {

    public static void main(String[] args) {
        int n = 19;
        System.out.println(isHappy1(n));
    }

    //哈希表
    public static boolean isHappy1(int n) {
        Set<Integer> record = new HashSet<>();
        while (n != 1 && !record.contains(n)) {
            record.add(n);
            n = getNextNum(n);
        }
        return n == 1;
    }

    public static int getNextNum(int n) {
        int res = 0;
        while (n > 0) {
            int temp = n % 10;
            res += temp * temp;
            n = n / 10;
        }
        return res;
    }
}
